package com.studentsys.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class BaseDao<T> {
    private QueryRunner runner = new QueryRunner(new ComboPooledDataSource());
    //增删改
    public int adu(String sql,Object...params){
        try {
            return runner.update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }
    //查询单条数据
    public T queryOne(String sql,Class clazz,Object...params){
        try {
            return runner.query(sql,new BeanHandler<T>(clazz),params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //查询多条数据
    public List<T> queryAll(String sql,Class clazz,Object...params){
        try {
            return runner.query(sql,new BeanListHandler<T>(clazz),params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //查询单行单列数据
    public Object queryScalar(String sql,Object...params){
        try {
            return runner.query(sql,new ScalarHandler<>(),params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
